package com.meteor.downloader.videodownloader.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.meteor.downloader.videodownloader.databinding.ItemFormatItemBinding
import com.meteor.downloader.videodownloader.databinding.ItemLanguageSetItemBinding
import com.meteor.downloader.videodownloader.mdmdata.DataDownloadingMdm
import com.meteor.downloader.videodownloader.mdmdata.DataMdmLanguage

class AdapterFormatItem(
    private val mAdapterContext: Context,
    private val mListDoData: MutableList<DataDownloadingMdm>
) :
    RecyclerView.Adapter<AdapterFormatItem.HolderItem>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): HolderItem {
        val binding = ItemFormatItemBinding.inflate(
            LayoutInflater.from(mAdapterContext),
            parent,
            false
        )
        val holderItem = HolderItem(binding)
        holderItem.itemView.setOnClickListener {
            val data = mListDoData[holderItem.absoluteAdapterPosition]
            if (data.mSelectMdm) return@setOnClickListener
            for (item in mListDoData) {
                item.mSelectMdm = false
            }
            data.mSelectMdm = true
            notifyDataSetChanged()
        }
        return holderItem
    }

    override fun onBindViewHolder(
        holder: HolderItem,
        position: Int
    ) {
        val data = mListDoData[position]
        holder.mMdmBinding.apply {
            tvLanguageName.text = data.videoQualityMdm
            holder.itemView.isSelected = data.mSelectMdm
        }
    }

    override fun getItemCount(): Int {
        return mListDoData.size
    }

    inner class HolderItem(val mMdmBinding: ItemFormatItemBinding) :
        RecyclerView.ViewHolder(mMdmBinding.root)

}